home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / cagd_lib / cagdcoer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  8.5 KB  |  247 lines

  1. /******************************************************************************
  2. * CagdCoer.c - Handle point coercesions/conversions.                  *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Aug. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #include "cagd_loc.h"
  8.  
  9. /******************************************************************************
  10. * Coerce Srf/Crv Point from index Index of Points array of Type PType to E2   *
  11. * If however Index < 0 Points is considered single point.              *
  12. ******************************************************************************/
  13. void CagdCoerceToE2(CagdRType *E2Point, CagdRType *Points[CAGD_MAX_PT_SIZE],
  14.                             int Index, CagdPointType PType)
  15. {
  16.     CagdBType
  17.     IsRational = CAGD_IS_RATIONAL_PT(PType);
  18.     int i,
  19.         MaxCoord = CAGD_NUM_OF_PT_COORD(PType);
  20.     CagdRType *Point;
  21.  
  22.     if (MaxCoord > 2)
  23.     MaxCoord = 2;
  24.  
  25.     if (Index < 0) {                  /* Points is one single point. */
  26.         Point = *Points;
  27.     for (i = 1; i <= MaxCoord; i++)
  28.         *E2Point++ = IsRational ? Point[i] / Point[W] : Point[i];
  29.     }
  30.     else                         /* Points is a full arrays from Srf or Crv. */
  31.     for (i = 1; i <= MaxCoord; i++)
  32.         *E2Point++ = IsRational ? Points[i][Index] / Points[W][Index] :
  33.                       Points[i][Index];
  34.  
  35.     for (i = MaxCoord + 1; i <= 2; i++)
  36.     *E2Point++ = 0.0;
  37. }
  38.  
  39. /******************************************************************************
  40. * Coerce Srf/Crv Point from index Index of Points array of Type PType to E3   *
  41. * If however Index < 0 Points is considered single point.              *
  42. ******************************************************************************/
  43. void CagdCoerceToE3(CagdRType *E3Point, CagdRType *Points[CAGD_MAX_PT_SIZE],
  44.                         int Index, CagdPointType PType)
  45. {
  46.     CagdBType
  47.     IsRational = CAGD_IS_RATIONAL_PT(PType);
  48.     int i,
  49.     MaxCoord = CAGD_NUM_OF_PT_COORD(PType);
  50.     CagdRType *Point;
  51.  
  52.     if (MaxCoord > 3)
  53.     MaxCoord = 3;
  54.  
  55.     if (Index < 0) {                  /* Points is one single point. */
  56.     Point = *Points;
  57.     for (i = 1; i <= MaxCoord; i++)
  58.         *E3Point++ = IsRational ? Point[i] / Point[W] : Point[i];
  59.     }
  60.     else                         /* Points is a full arrays from Srf or Crv. */
  61.     for (i = 1; i <= MaxCoord; i++)
  62.         *E3Point++ = IsRational ? Points[i][Index] / Points[W][Index] :
  63.                       Points[i][Index];
  64.  
  65.     for (i = MaxCoord + 1; i <= 3; i++)
  66.     *E3Point++ = 0.0;
  67. }
  68.  
  69. /******************************************************************************
  70. * Coerce Srf/Crv Point from index Index of Points array of Type PType to P2   *
  71. * If however Index < 0 Points is considered single point.              *
  72. ******************************************************************************/
  73. void CagdCoerceToP2(CagdRType *P2Point, CagdRType *Points[CAGD_MAX_PT_SIZE],
  74.                             int Index, CagdPointType PType)
  75. {
  76.     CagdBType
  77.     IsRational = CAGD_IS_RATIONAL_PT(PType);
  78.     int i,
  79.         MaxCoord = CAGD_NUM_OF_PT_COORD(PType);
  80.     CagdRType *Point;
  81.  
  82.     if (MaxCoord > 2)
  83.     MaxCoord = 2;
  84.  
  85.     if (Index < 0) {                  /* Points is one single point. */
  86.         Point = *Points;
  87.     *P2Point++ = IsRational ? Point[W] : 1.0;
  88.     for (i = 1; i <= MaxCoord; i++)
  89.         *P2Point++ = Point[i];
  90.     }
  91.     else {                       /* Points is a full arrays from Srf or Crv. */
  92.     *P2Point++ = IsRational ? Points[W][Index] : 1.0;
  93.     for (i = 1; i <= MaxCoord; i++)
  94.         *P2Point++ = Points[i][Index];
  95.     }
  96.  
  97.     for (i = MaxCoord + 1; i <= 2; i++)
  98.     *P2Point++ = 0.0;
  99. }
  100.  
  101. /******************************************************************************
  102. * Coerce Srf/Crv Point from index Index of Points array of Type PType to P3   *
  103. * If however Index < 0 Points is considered single point.              *
  104. ******************************************************************************/
  105. void CagdCoerceToP3(CagdRType *P3Point, CagdRType *Points[CAGD_MAX_PT_SIZE],
  106.                         int Index, CagdPointType PType)
  107. {
  108.     CagdBType
  109.     IsRational = CAGD_IS_RATIONAL_PT(PType);
  110.     int i,
  111.     MaxCoord = CAGD_NUM_OF_PT_COORD(PType);
  112.     CagdRType *Point;
  113.  
  114.     if (MaxCoord > 3)
  115.     MaxCoord = 3;
  116.  
  117.     if (Index < 0) {                  /* Points is one single point. */
  118.     Point = *Points;
  119.     *P3Point++ = IsRational ? Point[W] : 1.0;
  120.     for (i = 1; i <= MaxCoord; i++)
  121.         *P3Point++ = Point[i];
  122.     }
  123.     else {                       /* Points is a full arrays from Srf or Crv. */
  124.     *P3Point++ = IsRational ? Points[W][Index] : 1.0;
  125.     for (i = 1; i <= MaxCoord; i++)
  126.         *P3Point++ = Points[i][Index];
  127.     }
  128.  
  129.     for (i = MaxCoord + 1; i <= 3; i++)
  130.     *P3Point++ = 0.0;
  131. }
  132.  
  133. /******************************************************************************
  134. * Coerce Srf/Crv Point from index Index of Points array of Type PType to a    *
  135. * new type NewPType. If however Index < 0 Points is considered single point.  *
  136. ******************************************************************************/
  137. void CagdCoercePointTo(CagdRType *NewPoint, CagdPointType NewPType,
  138.                CagdRType *Points[CAGD_MAX_PT_SIZE], int Index,
  139.                CagdPointType OldPType)
  140. {
  141.     CagdBType
  142.     IsRational = CAGD_IS_RATIONAL_PT(OldPType),
  143.     NewIsRational = CAGD_IS_RATIONAL_PT(NewPType);
  144.     int i,
  145.     MaxCoord = CAGD_NUM_OF_PT_COORD(OldPType),
  146.     NewMaxCoord = CAGD_NUM_OF_PT_COORD(NewPType);
  147.     CagdRType *Point, Weight;
  148.  
  149.     if (MaxCoord > NewMaxCoord)
  150.     MaxCoord = NewMaxCoord;
  151.  
  152.     if (Index < 0) {                  /* Points is one single point. */
  153.         Point = *Points;
  154.     Weight = IsRational ? Point[W] : 1.0;
  155.     if (NewIsRational) {
  156.         *NewPoint++ = Weight;
  157.         Weight = 1.0;
  158.     }
  159.     for (i = 1; i <= MaxCoord; i++)
  160.         *NewPoint++ = Point[i] / Weight;
  161.     }
  162.     else {                       /* Points is a full arrays from Srf or Crv. */
  163.     Weight = IsRational ? Points[W][Index] : 1.0;
  164.     if (NewIsRational) {
  165.         *NewPoint++ = Weight;
  166.         Weight = 1.0;
  167.     }
  168.     for (i = 1; i <= MaxCoord; i++)
  169.         *NewPoint++ = Points[i][Index] / Weight;
  170.     }
  171.  
  172.     for (i = MaxCoord + 1; i <= NewMaxCoord; i++)
  173.     *NewPoint++ = 0.0;
  174. }
  175.  
  176. /******************************************************************************
  177. * Coerce Points array of point type OldPType to point type NewPType, in place *
  178. ******************************************************************************/
  179. void CagdCoercePointsTo(CagdRType *Points[], int Len,
  180.             CagdPointType OldPType, CagdPointType NewPType)
  181. {
  182.     int i, j,
  183.     OldIsRational = CAGD_IS_RATIONAL_PT(OldPType),
  184.     OldNumOfCoords = CAGD_NUM_OF_PT_COORD(OldPType),
  185.     NewIsRational = CAGD_IS_RATIONAL_PT(NewPType),
  186.     NewNumOfCoords = CAGD_NUM_OF_PT_COORD(NewPType);
  187.     CagdRType *NewPoints[CAGD_MAX_PT_SIZE], Pt[CAGD_MAX_PT_SIZE];
  188.  
  189.     for (i = !NewIsRational; i <= NewNumOfCoords; i++)
  190.     NewPoints[i] = (CagdRType *) IritMalloc(sizeof(CagdRType) * Len);
  191.  
  192.     for (i = 0; i < Len; i++) {
  193.     CagdCoercePointTo(Pt, NewPType, Points, i, OldPType);
  194.  
  195.     if (NewIsRational)
  196.         for (j = 0; j <= NewNumOfCoords; j++)
  197.         NewPoints[j][i] = Pt[j];
  198.     else
  199.         for (j = 1; j <= NewNumOfCoords; j++)
  200.         NewPoints[j][i] = Pt[j - 1];
  201.     }
  202.  
  203.     /* Replace old rep. with new. */
  204.     for (i = !OldIsRational; i <= OldNumOfCoords; i++)
  205.     IritFree((VoidPtr) Points[i]);
  206.     for (i = !NewIsRational; i <= NewNumOfCoords; i++)
  207.     Points[i] = NewPoints[i];
  208.     for (; i <= CAGD_MAX_PT_COORD; i++)
  209.     Points[i] = NULL;
  210. }
  211.  
  212. /******************************************************************************
  213. * Coerce Crv to point type PType.                          *
  214. ******************************************************************************/
  215. CagdCrvStruct *CagdCoerceCrvTo(CagdCrvStruct *Crv, CagdPointType PType)
  216. {
  217.     Crv = CagdCrvCopy(Crv);
  218.     CagdCoercePointsTo(Crv -> Points, Crv -> Length, Crv -> PType, PType);
  219.     Crv -> PType = PType;
  220.     return Crv;
  221. }
  222.  
  223. /******************************************************************************
  224. * Coerce Srf to point type PType.                          *
  225. ******************************************************************************/
  226. CagdSrfStruct *CagdCoerceSrfTo(CagdSrfStruct *Srf, CagdPointType PType)
  227. {
  228.     Srf = CagdSrfCopy(Srf);
  229.     CagdCoercePointsTo(Srf -> Points, Srf -> ULength * Srf -> VLength,
  230.                Srf -> PType, PType);
  231.     Srf -> PType = PType;
  232.     return Srf;
  233. }
  234.  
  235. /******************************************************************************
  236. * Returns the point which includes the two provided.                  *
  237. ******************************************************************************/
  238. CagdPointType CagdMergePointType(CagdPointType PType1, CagdPointType PType2)
  239. {
  240.     CagdBType
  241.     IsRational = CAGD_IS_RATIONAL_PT(PType1) || CAGD_IS_RATIONAL_PT(PType2);
  242.     int NumCoords = MAX(CAGD_NUM_OF_PT_COORD(PType1),
  243.                 CAGD_NUM_OF_PT_COORD(PType2));
  244.  
  245.     return CAGD_MAKE_PT_TYPE(IsRational, NumCoords);
  246. }
  247.